package com.antler.smt.portal.controller;

import com.antler.smt.entity.OrderEntity;
import com.antler.smt.entity.SupplierEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.annotation.OperateCut;
import com.antler.smt.request.admin.GetRequirementListReq;
import com.antler.smt.request.portal.OrderReq;
import com.antler.smt.response.admin.GetUserListResp;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.MatchUserService;
import com.antler.smt.service.portal.OrderService;
import com.antler.smt.service.portal.RequirementService;
import com.antler.smt.service.portal.SupplierService;
import com.antler.smt.utils.LogUtil;
import com.antler.smt.validate.DataValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 发布需求
 *
 * @author zhangyi
 * @date 2018/3/23 10:50
 */
@RestController
@RequestMapping(value = "/requirement")
@Api(description = "发布需求控制接口")
public class RequirementController extends BaseController{

    /**
     * 日志
     */
    private static Logger log = LogUtil.get();

    @Resource
    private RequirementService requirementService;

    @Resource
    private SupplierService supplierService;

    @Resource
    private MatchUserService matchUserService;

    @Resource
    private OrderService orderService;

    /**
     * 写入发布需求信息
     *
     * @param request 获取用户发布信息
     * @return 操作状态
     */
    @PostMapping("/insertRequirement")
    @ApiOperation(value = "写入发布需求信息", httpMethod = "POST")
    @OperateCut(operationContent = "采购商发布需求")
    public Result<Long> insertRequirement(@RequestBody GetRequirementListReq request){

        DataValidator.isNull(request,"发布需求实体不能为空！");
        DataValidator.isBlank(request.getName(),"项目名称必传！");
        DataValidator.isBlank(request.getDescription(),"项目描述必传！");
        DataValidator.isBlank(String.valueOf(request.getType()),"项目类型必传！");
        DataValidator.isBlank(String.valueOf(request.getDdtsBga()),"单点套数-BGA/0201焊盘必传！");
        DataValidator.isBlank(String.valueOf(request.getDdtsDipDls()),"单点套数-BGA/0201焊盘必传！");
        DataValidator.isBlank(String.valueOf(request.getDdtsDipHp()),"项单点套数-DIP焊盘必传！");
        DataValidator.isBlank(String.valueOf(request.getDdtsDipHx()),"单点套数-dip焊线必传！");
        DataValidator.isBlank(String.valueOf(request.getDdtsHp0402()),"单点套数-0402及以上焊盘必传！");
        DataValidator.isBlank(String.valueOf(request.getWlCount()),"物料-数量必传！");
        DataValidator.isBlank(String.valueOf(request.getWlBz()),"物料-包装 1静电气泡袋包装必传！");
        DataValidator.isBlank(String.valueOf(request.getWlCl()),"物料-材料 1有铅锡膏 2无铅锡膏 3红胶必传！");
        DataValidator.isBlank(String.valueOf(request.getWlGwAttachmentId()),"钢网附件必传！");
        DataValidator.isBlank(String.valueOf(request.getWlJj()),"物料-夹具 1由服务商提供 2自备必传！");
        DataValidator.isBlank(String.valueOf(request.getWlQw()),"物料-铅网 1由服务商提供 2自备必传！");
        DataValidator.isBlank(String.valueOf(request.getWlTz()),"物料-贴装 1单面 2双面必传！");
        DataValidator.isBlank(String.valueOf(request.getEndlineType()),"交期类型-1普通 2加急必传！");
        DataValidator.isBlank(String.valueOf(request.getBzwjClqdAttachmentId()),"标准文件-材料清单附件必传！");
        DataValidator.isBlank(String.valueOf(request.getBzwjTzAttachmentId()),"标准文件-图纸-附件必传！");
        DataValidator.isBlank(String.valueOf(request.getBzwjZbwjAttachmentId()),"标准文件-坐标文件-附件必传！");
        DataValidator.isBlank(String.valueOf(request.getAoi()),"全测或者抽测必传！");

        DataValidator.isBlank(String.valueOf(request.getAddressId()),"收货地址id不能为空！");

        DataValidator.isBlank(String.valueOf(request.getAddress()),"收货地址必传！");
        DataValidator.isBlank(String.valueOf(request.getContactTel()),"收获人电话必传！");
        DataValidator.isBlank(String.valueOf(request.getContactMan()),"联系人必传！");

        Result result = null;
           result = requirementService.insertRequirement(request,currentMember.getId());
        return result;
    }

    /**
     * 匹配项目专员
     *
     * @param requirementId 获取此时发布需求的id
     * @return
     */
    @PostMapping("/matchUser")
    @ApiOperation(value = "匹配项目专员(放入后台，此接口待删除...)", httpMethod = "POST")
    @OperateCut(operationContent = "匹配项目专员")
    public Result<GetUserListResp> matchUser(@RequestParam(value = "requirementId") long requirementId){

        DataValidator.isBlank(String.valueOf(requirementId),"需求id不能为空！");

        Result result = null;
            result = matchUserService.getMatchUser(requirementId,currentMember.getId());
        return result;
    }

    /**
     * 匹配商务专员
     *
     * @param requirementId 获取此时发布需求的id
     * @return
     */
    @PostMapping("/matchBusinessAffairs")
    @ApiOperation(value = "匹配商务专员", httpMethod = "POST")
    @OperateCut(operationContent = "匹配商务专员")
    public Result<GetUserListResp> matchBusinessAffairs(@RequestParam(value = "requirementId") long requirementId){

        DataValidator.isBlank(String.valueOf(requirementId),"需求id不能为空！");

        Result result = null;
            result = matchUserService.matchBusinessAffairs(requirementId,currentMember.getId());
        return result;
    }

    /**
     * 根据订单显示服务商实体
     *
     * @param orderEntity 订单实体
     * @return
     */
    @PostMapping("/supplierInfo")
    @ApiOperation(value = "根据订单显示服务商实体", httpMethod = "POST")
    @OperateCut(operationContent = "查看服务商信息")
    public Result<SupplierEntity> getSupplierInfo(@RequestBody OrderEntity orderEntity){

        DataValidator.isNull(orderEntity,"订单实体不能为空！");

        Result result = null;
            result = supplierService.getSupplierInfo(orderEntity);
        return result;
    }



    /**
     * 采购商申请支付
     *
     * @param req 支付方式
     * @return
     */
    @PostMapping("/payStyle/{requirementId}")
    @ApiOperation(value = "采购商申请支付", httpMethod = "POST")
    @OperateCut(operationContent = "采购商申请支付")
    public Result<ResultCodeEnum> payStyle(@RequestBody OrderReq req,@PathVariable Long requirementId){

        DataValidator.isNull(req,"支付方式实体不能为空！");
        DataValidator.isBlank(String.valueOf(requirementId),"需求id不能为空！");
        DataValidator.isBlank(String.valueOf(req.getPayApplyType()),"申请支付方式不能为空！");
        DataValidator.isBlank(String.valueOf(req.getPayApplyAmmount()),"申请支付金额不能为空！");

        Result result = null;
            result = orderService.payStyle(req,requirementId);
        return result;
    }

    /**
     * 查看最终报价
     *
     * @return
     */
    @PostMapping("/lastOffer/{orderId}")
    @ApiOperation(value = "查看最终报价", httpMethod = "POST")
    @OperateCut(operationContent = "采购商查看最终报价")
    public Result<Long> lastOffer(@PathVariable Long orderId){

        DataValidator.isBlank(String.valueOf(orderId),"订单id不能为空！");

        Result result = null;
            result = orderService.lastOffer(orderId);
        return result;
    }
}
